const query = require('./query')

// 增加浏览量
exports.addGood = async (article_id, newGood) => {
  let sql = ` update article set good_count = ${newGood}  where article_id = ${article_id}`
  let result = await query(sql)
  return result
}

// 增加浏览量
exports.addViews = async (article_id, newViews) => {
  let sql = ` update article set views = ${newViews}  where article_id = ${article_id}`
  let result = await query(sql)
  return result
}

// 获得文章数量
exports.getCount = async (category_id, tag_id) => {
  let sql = ` select count(article_id) as count from article where 1`
  if (category_id) {
    sql += ` and category_id = ${category_id} `
  }
  if (tag_id) {
    sql += ` and FIND_IN_SET(${tag_id}, tag_ids) > 0 `
  }
  let result = await query(sql)
  return result[0].count
}

// 获取具体文章数据
exports.getArticleDetail = async (article_id) => {
  let sql = ` select title, content, views, good_count from article where article_id = ${article_id}`
  let result = await query(sql)
  return result[0]
}

// 获取文章数据
exports.getArticle = async (page, pagesize, category_id, tag_id) => {
  let offset = (page - 1) * pagesize
  let sql = ` select article_id, title, cover, views, good_count, content, intro from article where 1 `
  if (category_id) {
    sql += ` and category_id = ${category_id} `
  }
  if (tag_id) {
    sql += ` and FIND_IN_SET(${tag_id}, tag_ids) > 0 `
  }
  sql += ` limit ${offset}, ${pagesize}`
  let result = await query(sql)
  return result
}

// 获取标签数据
exports.getTag = async () => {
  let sql = `select tag_id, tag_name from tag where is_show = 1`
  let result = await query(sql)
  return result
}

// 获取分类数据
exports.getCategory = async () => {
  let sql = `select category_id, category_name from category where is_show = 1`
  let result = await query(sql)
  return result
}
